﻿using System.Collections.Generic;
using System.Data;
using CRM.Data.Common;
using CRM.Data.Interfaces;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration.Unity;
using Microsoft.Practices.Unity;

namespace CRM.Data
{
    public class BaseRepository<T> : IBaseRepository<T>
    {
        public BaseRepository()
        {
            var container = new UnityContainer();
            container.AddNewExtension<EnterpriseLibraryCoreExtension>();
            container.AddNewExtension<DataAccessBlockExtension>();
            //Change with SqlConnectionString as an when required in your application.
            db = container.Resolve<Database>();
        }

        public IList<T> Get(string StoredProcedureName, params object[] parameters)
        {   
            IDataReader dataReader = db.ExecuteReader(StoredProcedureName, parameters);
            List<T> list =  ListConversionExtension.ConvertToList<T>(dataReader);
            return list;
        }
        public readonly Database db;
    }
 }


    






   








//using (IDataReader reader = db.ExecuteReader("TEST_CRM.GET_SITE_NAME", DBNull.Value))
//{
//    if (reader != null)
//    {
//        List<CustomerEntity> customers = reader.Select
//            (reader => 
//             new CustomerEntity {  CustomerId = reader["id"] is DBNull ? (int) null : reader["id"].ToString(),
//                                     CustomerName = reader["name"] is DBNull ? null : reader["name"].ToString()     }).ToList();
//    }
//}
//return new List<CustomerEntity>   {     new CustomerEntity {CustomerId=1,CustomerName = "Laxman"},
        //                                                     new CustomerEntity {CustomerId=2,CustomerName = "Vivek"},
        //                                                     new CustomerEntity {CustomerId=3,CustomerName = "Vikas"},
        //                                                     new CustomerEntity {CustomerId=4,CustomerName = "Swapnil"},
        //                                                     new CustomerEntity {CustomerId=5,CustomerName = "Anthony"}
        //  
////OleDbConnection oleDbConnection = new OleDbConnection("Provider=msdaora;Data Source=Edcsquared105;Persist Security Info=True;User ID=tomball;Password=sa");
//OleDbConnection oleDbConnection = new OleDbConnection("Provider=SqlOleDb;Server=TPSLVL00131;Database=IDBI2ndJune;Uid=sa;Pwd=techprocess;Max Pool Size=256; Timeout=0");
//OleDbCommand oleDbCommand = new OleDbCommand("select * from tbl_Employee", oleDbConnection);
//oleDbConnection.Open();
//OleDbDataReader dataReader = oleDbCommand.ExecuteReader(CommandBehavior.CloseConnection);